package com.example.demo.controller;

import com.example.demo.common.AppVariable;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.ResultAjax;

import com.example.demo.model.UserInfo;
import com.example.demo.model.vo.UserInfoVO;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/08/01/10:38
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册功能
     * @param userInfo
     * @return
     */
    @RequestMapping("/reg")
    public ResultAjax reg(UserInfo userInfo){
        //1.效验参数
        if(userInfo==null || !StringUtils.hasLength(userInfo.getUsername())
                ||!StringUtils.hasLength(userInfo.getPassword())){
            //非法参数
            return ResultAjax.fail(-1,"非法参数");
        }
        List<String> list=userService.getUsername();
        if (list.contains(userInfo.getUsername())){
            return ResultAjax.fail(-2,"该用户名已存在！");
        }
        //对用户输入的密码进行加盐加密
        userInfo.setPassword(PasswordUtils.encrypt(userInfo.getPassword()));
        //2.将数据添加到数据库中 调用service接口
        int reg=userService.reg(userInfo);
        //3.将结果返回给前端
        return ResultAjax.succ(reg);
    }

    @RequestMapping("/login")
    public ResultAjax login(UserInfoVO userInfoVO, HttpServletRequest request){
        //1.参数效验
        if(userInfoVO==null || !StringUtils.hasLength(userInfoVO.getUsername())||
        !StringUtils.hasLength(userInfoVO.getPassword())){
            //非法参数
            return ResultAjax.fail(-1,"非法参数");
        }
        //2.使用用户名查询对象
        UserInfo userInfo=userService.getUserByName(userInfoVO.getUsername());
        if(userInfo==null || userInfo.getId() == 0){
            //不存在此用户
            return ResultAjax.fail(-2,"用户名或者密码错误");
        }
        //3.将用户传过来的密码与数据库的密码进行对比
        //userInfo.getPassword().equals(userInfoVO.getPassword())
        //加盐解密
        if(!PasswordUtils.decrypt(userInfoVO.getPassword(),userInfo.getPassword())){
            //密码不一致
            return ResultAjax.fail(-3,"用户名或者密码错误");
        }
        //4.比较成功之后将结果存储在session中
        HttpSession session=request.getSession();
        session.setAttribute(AppVariable.SESSION_USERINFO_KEY,userInfo);
        //5.将结果返回给前端
        return ResultAjax.succ(1);
    }

    /**
     * 退出
     */
    @RequestMapping("/logout")
    public ResultAjax logout(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session != null &&session.getAttribute(AppVariable.SESSION_USERINFO_KEY)!=null){
            session.removeAttribute(AppVariable.SESSION_USERINFO_KEY);
        }
        return ResultAjax.succ(1);
    }
}
